package com.project.biz.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.biz.CmsUserAddressBiz;
import com.project.common.exception.ServiceException;
import com.project.entity.CmsUserAddress;
import com.project.mapper.CmsUserAddressMapper;
import com.project.pojo.model.UserAddressAddOrEditModel;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Description: 地址管理业务层接口实现类
 * @Author: zhuchi
 * @CreateDate: 2020/10/15 1:49
 * @Version: 1.0
 */
@Service
public class CmsUserAddressBizImpl extends ServiceImpl<CmsUserAddressMapper, CmsUserAddress> implements CmsUserAddressBiz {

    /**
     * 根据用户ID分页获取收货地址
     * @param pageNo
     * @param pageSize
     * @param userId
     * @return
     */
    public IPage<CmsUserAddress> getAddress(int pageNo, int pageSize, Long userId){
        Page<CmsUserAddress> page = new Page<>(pageNo, pageSize);
        QueryWrapper<CmsUserAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        queryWrapper.eq("base_user_id", userId);
        queryWrapper.orderByDesc("create_time");
        return this.page(page, queryWrapper);
    }

    /**
     * 根据用户ID获取收货地址
     * @param userId
     * @return
     */
    public List<CmsUserAddress> getAddress(Long userId){
        QueryWrapper<CmsUserAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        queryWrapper.eq("base_user_id", userId);
        queryWrapper.orderByDesc("create_time");
        return this.list(queryWrapper);
    }

    /**
     * 获取用户默认收货地址
     * @param userId
     * @return
     */
    public CmsUserAddress getDefaultAddress(Long userId){
        QueryWrapper<CmsUserAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        queryWrapper.eq("base_user_id", userId);
        queryWrapper.eq("default_adress", 1);
        CmsUserAddress userAddress = this.getOne(queryWrapper);

        //如果当前用户无默认地址时，取最新添加的地址
        if (null == userAddress){
            List<CmsUserAddress> list = this.getAddress(userId);
            if (list.size() > 0){
                userAddress = list.get(0);
            }
        }
        return userAddress;
    }

    /**
     * 新增/修改收货地址
     * @param addOrEditDTO
     * @return
     */
    @Transactional(rollbackFor = Exception.class , isolation = Isolation.REPEATABLE_READ, timeout = 30)
    public Long addOrEditAddress(UserAddressAddOrEditModel addOrEditDTO){
        int result = 0;
        Long addressId = null;
        String all_adress = addOrEditDTO.getProvinceName() + addOrEditDTO.getCityName() + addOrEditDTO.getCountyName() + addOrEditDTO.getTown();

        //默认地址时,先更新当前用户之前的默认地址为非默认
        if (null != addOrEditDTO.getDefaultAdress()){
            if (addOrEditDTO.getDefaultAdress() == 1){
                this.baseMapper.updateForNoDefault(addOrEditDTO.getBaseUserId());
            }
        }
        CmsUserAddress userAddress = new CmsUserAddress();
        if (null == addOrEditDTO.getAddressId()){//新增
            BeanUtils.copyProperties(addOrEditDTO, userAddress);

            userAddress.setAllAdress(all_adress);
            userAddress.setCreateTime(new Date());
            userAddress.setCreateUser(addOrEditDTO.getBaseUserId());
            userAddress.setStatus(1);
            result = this.baseMapper.insert(userAddress);
            if (result > 0){
                addressId = userAddress.getAddressId();
            }
        }else{
            userAddress = this.getById(addOrEditDTO.getAddressId());
            if (null == userAddress){
                throw new ServiceException("修改的收货地址信息不存在!");
            }
            BeanUtils.copyProperties(addOrEditDTO, userAddress);

            userAddress.setAllAdress(all_adress);
            userAddress.setUpdateUser(addOrEditDTO.getBaseUserId());
            userAddress.setUpdateTime(new Date());
            result = this.baseMapper.updateById(userAddress);
            return result > 0 ? userAddress.getAddressId() : null;
        }

        return addressId;
    }

}
